<?php

/**
 * DB Manipulator
 *
 *
 * Structure of $dataArray:
 *
 * $dataArray=array(
 *    'field_1'=>'value_1',
 *    'field_2'=>'value_2'
 *    );
 *
 * @author RD
 */
Class RD_DB
{
	var $db_host;
	var $db_user;
	var $db_password;
	var $db_name;
	var $db_charset;
	var $is_debug;

	private $link_id;

	/**
	 * Constructor
	 */
	public function __construct($db_host,$db_user,$db_password,$db_name,$db_charset='utf8',$is_debug=false)
	{
		$this->db_host = $db_host;
		$this->db_user = $db_user;
		$this->db_password = $db_password;
		$this->db_name = $db_name;
		$this->db_charset = $db_charset;
		$this->is_debug = $is_debug;

		$this->connect();
	}

	/**
	 * Connect to database
	 */
	private function connect()
	{
		$this->link_id = @mysql_connect($this->db_host, $this->db_user, $this->db_password, true);
		if(!$this->link_id)
		{
			$this->halt("数据库连接失败");
		}

		if(!@mysql_select_db($this->db_name,$this->link_id)) 
		{
			$this->halt('数据库选择失败');
		}

		@mysql_query("set names ".$this->db_charset);
	}

	/**
	 * Excute the given statement
	 * @param unknown_type $sql
	 */
	public function query($sql) 
	{
		$this->write_log("Query = ".$sql);

		$query = mysql_query($sql,$this->link_id);

		if(!$query) $this->halt('Query Error: ' . $sql);

		return $query;
	}

	/**
	 * Get one record
	 * @param unknown_type $sql
	 * @param unknown_type $result_type （MYSQL_ASSOC，MYSQL_NUM，MYSQL_BOTH）
	 */
	public function get_row($sql,$result_type = MYSQL_ASSOC) {

		$query = $this->query($sql);

		$rt = mysql_fetch_array($query,$result_type);

		$this->write_log("获取一条记录 ".$sql);

		return $rt;
	}
	
	/**
	 * Get all records
	 * @param unknown_type $sql
	 * @param unknown_type $result_type
	 */
	public function get_results($sql,$result_type = MYSQL_ASSOC) 
	{
		$query = $this->query($sql);

		$i = 0;

		$rt = array();

		while($row =mysql_fetch_array($query,$result_type)) 
		{
			$rt[$i]=$row;
			$i++;
		}

		$this->write_log("获取全部记录 ".$sql);

		return $rt;
	}

	/**
	 * Insert record
	 * @param unknown_type $table
	 * @param unknown_type $dataArray
	 */
	public function insert($table,$dataArray) 
	{
		$field = "";
		$value = "";

		if( !is_array($dataArray) || count($dataArray)<=0) 
		{
			$this->halt('没有要插入的数据');
			return false;
		}

		while(list($key,$val)=each($dataArray)) 
		{
			$field .="$key,";
			$value .="'$val',";
		}

		$field = substr( $field,0,-1);
		$value = substr( $value,0,-1);

		$sql = "insert into $table($field) values($value)";

		$this->write_log("插入 ".$sql);

		if(!$this->query($sql)) return false;

		return true;

	}

	/**
	 * Update records
	 * @param unknown_type $table
	 * @param unknown_type $dataArray
	 * @param unknown_type $condition
	 */
	public function update( $table, $dataArray, $condition="") 
	{
		if( !is_array($dataArray) || count($dataArray)<=0) 
		{
			$this->halt('没有要更新的数据');
			return false;
		}

		$value = "";

		while( list($key,$val) = each($dataArray))
		{
			$value .= "$key = '$val',";
		}
		
		$value .= substr( $value,0,-1);

		$sql = "update $table set $value where 1=1 and $condition";

		$this->write_log("更新 ".$sql);

		if(!$this->query($sql)) return false;

		return true;

	}

	/**
	 * Delete records
	 * @param unknown_type $table
	 * @param unknown_type $condition
	 */
	public function delete( $table,$condition="") 
	{
		$sql = "delete from $table where 1=1 and $condition";
		
		$this->write_log("删除 ".$sql);
		
		if(!$this->query($sql)) return false;
		
		return true;
	}

	/**
	 * Return the result set
	 * @param unknown_type $query
	 * @param unknown_type $result_type
	 */
	public function fetch_array($query, $result_type = MYSQL_ASSOC)
	{
		$this->write_log("返回结果集");
		return mysql_fetch_array($query, $result_type);
	}

	/**
	 * Get count of records
	 * @param unknown_type $results
	 */
	public function num_rows($results) 
	{
		if(!is_bool($results)) 
		{
			$num = mysql_num_rows($results);
			$this->write_log("获取的记录条数为".$num);
			return $num;
		} 
		else 
		{
			return 0;
		}
	}

	/**
	 * Release the result set
	 */
	public function free_result() 
	{
		$void = func_get_args();
		foreach($void as $query) 
		{
			if(is_resource($query) && get_resource_type($query) === 'mysql result') 
			{
				return mysql_free_result($query);
			}
		}
		$this->write_log("释放结果集");
	}

	/**
	 * Get the last insert id
	 */
	public function get_last_insert_id() 
	{
		$id = mysql_insert_id($this->link_id);
		$this->write_log("最后插入的id为".$id);
		return $id;
	}

	/**
	 * Close connection
	 */
	protected function close() 
	{
		$this->write_log("已关闭数据库连接");
		return @mysql_close($this->link_id);
	}

	/**
	 * Error
	 * @param unknown_type $msg
	 */
	private function halt($msg='') 
	{
		$msg .= "\r\n".mysql_error();
		$this->write_log($msg);
		die($msg);
	}

	/**
	 * Destructor
	 */
	public function __destruct() 
	{
		$this->free_result();
	}

	/**
	 * Output the log info
	 * @param unknown_type $msg
	 */
	public function write_log($msg='')
	{
		if($this->is_debug)
		{
			$text = $msg."<br>";
			echo $text;
		}
	}
}

?>
